package com.cdshangpai.photoapp.ui.caseui;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.cdshangpai.photoapp.R;
import com.cdshangpai.photoapp.databinding.ActivityCaseListBinding;
import com.cdshangpai.photoapp.ui.CaseFilter;
import com.cdshangpai.photoapp.ui.OnListViewClickListener;
import com.cdshangpai.utils.db.CaseDb;
import com.cdshangpai.utils.db.beans.Case;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;

import java.util.List;

public class CaseListActivity extends AppCompatActivity  {

    ActivityCaseListBinding binding;
    CaseFilter caseFilter=new CaseFilter();
    CaseDb caseDb;

    private OnListViewClickListener onListViewClickListener=new OnListViewClickListener() {
        @Override
        public void onClick(Integer position) {
            Case cas=binding.getData().adapter.datas.get(position);
        }
    };

    private void search()
    {
        if(caseDb==null)
        {
            caseDb=new CaseDb();
        }

        List<Case> cases=caseDb.query(caseFilter.area,caseFilter.city,caseFilter.style,caseFilter.color,caseFilter.price);


        binding.getData().adapter.datas=cases;
        binding.caseListView.setAdapter(binding.getData().getAdapter());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_case_list);
        initData();
    }

    private FlexboxLayoutManager getFlexboxLayoutManager(Boolean isCenter)
    {
        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this);
        layoutManager.setFlexWrap(FlexWrap.WRAP);
        layoutManager.setFlexDirection(FlexDirection.ROW);
        if(isCenter)
        {
            layoutManager.setJustifyContent(JustifyContent.CENTER);
            layoutManager.setAlignItems(AlignItems.CENTER);
        }
        else {
            layoutManager.setAlignItems(AlignItems.STRETCH);
            layoutManager.setJustifyContent(JustifyContent.FLEX_START);
        }
        return layoutManager;
    }

    /**
     * 初始化案例数据
     */
    private void initData() {
        CaseListUiData caseListUiData = new CaseListUiData();
        ImageListAdapter imageListAdapter = new ImageListAdapter(this,onListViewClickListener);
        caseListUiData.adapter = imageListAdapter;
        binding.setData(caseListUiData);

        FlexboxLayoutManager layoutManager=getFlexboxLayoutManager(false);
        binding.caseListView.setLayoutManager(layoutManager);

        search();
    }
}